﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Threading;
using System.ComponentModel;
using RTracker.Models.User;
using System.Configuration;
using System.Threading.Tasks;

namespace RTracker.Models.Common
{
    public class MEmail
    {
        /// <summary>
        /// Sends confirmation email to the user when new account is created.
        /// </summary>
        /// <param name="objCUser">User</param>
        /// <returns></returns>
        public void SendConfirmationEmail(string strEmail, string strPassword)
        {
            MailMessage mMessage = new MailMessage();
            MailAddress mMessageTo = new MailAddress(strEmail);
            MailAddress mMessageFrom = new MailAddress("slinfydemo@gmail.com", "SLINFY HR");
            mMessage.To.Add(mMessageTo);
            mMessage.From = mMessageFrom;
            mMessage.Subject = "Account Confirmation Email";
            mMessage.Body = "Hello there.<br /> Your account has been created and your password is <b>" + strPassword
                            + "<b>. <br />Use the above password to log in and change your password."; // body should be dynamically generated from canned responses
            mMessage.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["SmtpHost"]);
            smtp.Credentials = new System.Net.NetworkCredential(
                                    ConfigurationManager.AppSettings["NoReplyEmailId"],
                                    ConfigurationManager.AppSettings["NoReplyEmailPassword"]);
            smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]);
            smtp.EnableSsl = true;

            try
            {
                smtp.SendAsync(mMessage, null);
            }
            catch
            {
                // add error to log
            }
        }
    }
}